//
//  xpqTableViewController.m
//  refreshTest
//
//  Created by boc on 15/11/20.
//  Copyright © 2015年 bocweb. All rights reserved.

#import "xpqTableViewController.h"
#import "cellViewController.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
@interface xpqTableViewController ()
@property (assign, nonatomic) int count;
//@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *data;
@end
#define MJRandomData [NSString stringWithFormat:@"随机数据---%d", arc4random_uniform(100)]
@implementation xpqTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.count = 0;
    
    __unsafe_unretained typeof(self) weakSelf = self;
//    __unsafe_unretained UITableView *tableView = self.tableView;
//    
////    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
////        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
////            weakSelf.count += 12;
////            [tableView reloadData];
////            [tableView.mj_header endRefreshing];
////        });
////    }];
////    tableView.mj_header.automaticallyChangeAlpha = YES;
////    
//    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            weakSelf.count += 5;
//            [tableView reloadData];
//            [tableView.mj_footer endRefreshing];
//        });
//    }];
//    footer.hidden = YES;
//    tableView.mj_footer = footer;

    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 马上进入刷新状态
    [header beginRefreshing];
    
    // 设置header
    self.tableView.mj_header = header;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", indexPath.row % 2?@"push":@"modal", self.data[indexPath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cellViewController *test = [[cellViewController alloc] init];
    if (indexPath.row % 2 && self.navigationController) {
        [self.navigationController pushViewController:test animated:YES];
    } else {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
        [self presentViewController:nav animated:YES completion:nil];
    }
}

- (void)loadNewData
{
    // 1.添加假数据
    for (int i = 0; i<5; i++) {
        [self.data insertObject:MJRandomData atIndex:0];
    }

        // 刷新表格
        [self.tableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.mj_header endRefreshing];
    
}
- (NSMutableArray *)data
{
    if (!_data) {
        self.data = [NSMutableArray array];
        for (int i = 0; i<5; i++) {
            [self.data addObject:MJRandomData];
        }
    }
    return _data;
}
@end
